package com.ts.util;

import com.ts.common.constant.InsUpdDelFlagConstant;
import com.ts.proj.entity.SystemColumnEntity;
import com.ts.proj.entity.SystemFieldEntity;
import com.ts.proj.service.SystemColumnService;
import com.ts.proj.service.SystemFieldService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class FieldListUtil {

    public static PageData fieldsList(SystemColumnService systemColumnService, String tableName, SystemFieldService systemFieldService){
        PageData result = new PageData();
        List<PageData> attributes = new ArrayList<>();
        List<PageData> freezAttributes = new ArrayList<>();
        List<PageData> dlgAttributes = new ArrayList<>();
        PageData pd = new PageData();
        pd.put("className", tableName);
        pd.put("deleteFlag", InsUpdDelFlagConstant.DELETE_FLAG_N);
        List<SystemColumnEntity> all = systemColumnService.findAll(pd);
        if(all.size()==1){
            SystemColumnEntity columnEntity = all.get(0);
            List<String> allList = Array2List.transferArrayToList(columnEntity.getAllName().trim().split(" "));
            List<String> dlgAllList = Array2List.transferArrayToList(columnEntity.getAllName().trim().split(" "));
            List<String> freezeList = new ArrayList<>();
            List<String> hiddenList = new ArrayList<>();
            if(columnEntity.getFreezeName()!=null){
                freezeList = Array2List.transferArrayToList(columnEntity.getFreezeName().trim().split(" "));
            }
            if(columnEntity.getHiddenName()!=null){
                hiddenList = Array2List.transferArrayToList(columnEntity.getHiddenName().trim().split(" "));
            }
            //冻结的去掉隐藏的
            freezeList.removeAll(hiddenList);
            allList.removeAll(hiddenList);
            allList.removeAll(freezeList);
            dlgAllList.remove("id");
            dlgAllList.remove("deleteFlag");
            dlgAllList.remove("userId");
            dlgAllList.remove("belongTo");
            dlgAllList.remove("createTime");
            dlgAllList.remove("updateTime");
            PageData tmpPd = new PageData();
            tmpPd.put("deleteFlag", InsUpdDelFlagConstant.DELETE_FLAG_N);
            tmpPd.put("tableName", tableName);
            attributes = EntityAttributeUtil.entityAttributeWithZH(allList,systemFieldService.findAll(tmpPd));
            if(freezeList.size()!=0&&!freezeList.get(0).equals("")){
                freezAttributes = EntityAttributeUtil.entityAttributeWithZH(freezeList,systemFieldService.findAll(tmpPd));
            }
            if(attributes.size()==0){
                try {
                    Class<?> aClass = Class.forName(tableName);
                    attributes = EntityAttributeUtil.entityAttributeWithZH(aClass);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                    attributes =  EntityAttributeUtil.entityAttributeWithZH(SystemFieldEntity.class);
                }
            }
            dlgAttributes = EntityAttributeUtil.entityAttributeWithZH(dlgAllList, systemFieldService.findAll(tmpPd));
        }else {
            try {
                Class<?> aClass = Class.forName("com.ts.proj.entity." + tableName);
                attributes = EntityAttributeUtil.entityAttributeWithZH(aClass);
                dlgAttributes = attributes;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                attributes = EntityAttributeUtil.entityAttributeWithZH(SystemFieldEntity.class);
            }

        }
        result.put("all", attributes);
        result.put("freeze", freezAttributes);

        result.put("dlg", dlgAttributes);
        return result;
    }
}
